using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	#endregion

	public partial class FrmQuanLyPhieuDangKy : Office2007Form
	{
		public EventHandler Handler { get; set; }
        private int MaKhoa;
        private int MaCN;
        private int MaKHDT;

        public FrmQuanLyPhieuDangKy(int makhoa, int macn, int makhdt)
		{
			InitializeComponent();
            this.MaKhoa = makhoa;
            this.MaCN = macn;
            this.MaKHDT = makhdt;
		}

		private void FrmQuanLyPhieuDangKy_Load(object sender, EventArgs e)
		{
			InitGridView();
			LoadData();
			dtgvData.ClearSelection();
		}

		private void btnThem_Click(object sender, EventArgs e)
		{
			FrmLapPhieuDangKyHocPhan frm = new FrmLapPhieuDangKyHocPhan(this.MaKhoa, this.MaCN, this.MaKHDT);
			frm.Handler += LoadData;
			frm.ShowDialog();
		}

		private void btnXoa_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
				if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					if (PhieuDangKyRepository.Delete(int.Parse(dtgvData.SelectedRows[0].Cells["MaPhieuDK"].Value.ToString())))
					{
						MessageBoxEx.Show("Xóa dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
						LoadData();
					}
					else
					{
						MessageBoxEx.Show("Xóa dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}

		private void btnCapNhat_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
                FrmLapPhieuDangKyHocPhan frm = new FrmLapPhieuDangKyHocPhan(this.MaKhoa, this.MaCN, this.MaKHDT, int.Parse(dtgvData.SelectedRows[0].Cells["MaPhieuDK"].Value.ToString()));
                frm.Handler += LoadData;
                frm.ShowDialog();
			}
		}

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "MaPhieuDK",
				DataPropertyName = "MaPhieuDK",
				Name = "MaPhieuDK",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4)) - 30
			};
			dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "KHDT",
                DataPropertyName = "KHDT",
                Name = "KHDT",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 4)) + 60
            };
            dtgvData.Columns.Add(objColumn);

			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "MSSV",
				DataPropertyName = "MSSV",
				Name = "MSSV",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4)) - 30
			};
			dtgvData.Columns.Add(objColumn);
			
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "NgayDangKy",
				DataPropertyName = "NgayDangKy",
				Name = "NgayDangKy",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4)) - 1
			};
			dtgvData.Columns.Add(objColumn);
		}

		private void LoadData(object sender, EventArgs e)
		{
			LoadData();
		}

		private void LoadData()
		{
			var lstItem = PhieuDangKyRepository.SelectByMaKHDT(this.MaKHDT);
			var dt = new DataTable();
			dt.Columns.Add("MaPhieuDK");
            dt.Columns.Add("KHDT");
			dt.Columns.Add("MSSV");
			dt.Columns.Add("NgayDangKy");
			for (int i = 0; i < lstItem.Count; i++)
			{
                dt.Rows.Add(lstItem[i].MaPhieuDK, lstItem[i].KeHoachDaoTao.TenGoi, lstItem[i].MSSV, String.Format("{0:dd/MM/yyyy}", lstItem[i].NgayDangKy));
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

		private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
            if (dtgvData.SelectedRows.Count > 0)
            {
                FrmLapPhieuDangKyHocPhan frm = new FrmLapPhieuDangKyHocPhan(this.MaKhoa, this.MaCN, this.MaKHDT, int.Parse(dtgvData.SelectedRows[0].Cells["MaPhieuDK"].Value.ToString()));
                frm.Handler += LoadData;
                frm.ShowDialog();
            }
		}

	}

}
